package com.ruoyi.firstPage;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.application.domain.Application;
import com.ruoyi.application.service.IApplicationService;
import com.ruoyi.classes.domain.Classes;
import com.ruoyi.classes.service.IClassesService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.monitor.domain.Monitor;
import com.ruoyi.monitor.service.IMonitorService;
import com.ruoyi.school.domain.School;
import com.ruoyi.school.service.ISchoolService;
import com.ruoyi.student.domain.Student;
import com.ruoyi.student.service.IStudentService;
import com.ruoyi.system.service.ISysUserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: intelligent-monitor
 * @package: com.ruoyi.firstPage
 * @className: topGetController
 * @author: 寇耕耀
 */

@RestController
@RequestMapping("/firstPage/firstPage")
public class FirstPageController extends BaseController {
    @Autowired
    private ISchoolService schoolService;
    @Autowired
    private IApplicationService applicationService;
    @Autowired
    private IMonitorService monitorService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IClassesService classesService;
    @Autowired
    private IStudentService studentService;

    /**
     * 获取申请数、学校数和监控率
     *
     * @return
     */
    @GetMapping("/getAdminTop")
    public AjaxResult getAdminTop() {
//        获取学校数
        List<School> schoolList = schoolService.selectSchoolList(new School());
        int schoolCount = schoolList.size();

//        获取待处理申请数
        List<Application> applications = applicationService.selectApplicationList(new Application());
        applications.removeIf(ObjectUtils::isNotNull);
        int applicationCount = applications.size();

//        获取监控正常率
        List<Monitor> monitors = monitorService.selectMonitorList(new Monitor());
        List<Monitor> usingMonitors = monitors.stream().filter(m -> "1".equals(m.getCameraStatus())).collect(Collectors.toList());
        int monitorRate = (100 * usingMonitors.size()) / (monitors.size());

        return AjaxResult.success(new int[]{applicationCount, schoolCount, monitorRate});
    }

    /**
     * 获取班级数、学生数和监控率
     */
    @GetMapping("/getSchoolTop")
    public AjaxResult getSchoolTop(School school) {
//        获取班级数
        Classes classes = new Classes();
        classes.setClassBelonging(school.getSchoolName());
        int classCount = classesService.selectClassesList(classes).size();

//        获取学生数
        Student student = new Student();
        student.setStuSchool(school.getSchoolName());
        int studentCount = studentService.selectStudentList(student).size();

//        获取监控率
        Monitor monitor = new Monitor();
        monitor.setSchoolName(school.getSchoolName());
        List<Monitor> monitors = monitorService.selectMonitorList(monitor);
        List<Monitor> usingMonitors = monitors.stream().filter(m -> "1".equals(m.getCameraStatus())).collect(Collectors.toList());
        int monitorRate = (100 * usingMonitors.size()) / (monitors.size());

        return AjaxResult.success(new int[]{classCount, studentCount, monitorRate});
    }

    /**
     * 获取省份学校数
     */
    @GetMapping("/getSchoolCountByProvince")
    public AjaxResult getSchoolCountByProvince() {
        ProvinceCountVo[] provinceCountVos = schoolService.selectSchoolList(new School())
                .stream()
//                对省市区字段截取省
                .peek(s -> s.setSchoolProvince(s.getSchoolProvince().split("-")[0]))
//                计数器分组
                .collect(Collectors.groupingBy(
//                        分组键：学校省份
                        School::getSchoolProvince,
//                        分组值：计数
                        Collectors.counting()
                ))
//                转换分组结果的每个键值对转成流
                .entrySet().stream()
//                将每个成流的键值对映射为ProvinceCountVo对象
                .map(entry -> new ProvinceCountVo(entry.getKey(), entry.getValue()))
//                收集为数组
                .toArray(ProvinceCountVo[]::new);

        return AjaxResult.success(provinceCountVos);
    }

    /**
     * 获取管理员团队
     */
    @GetMapping("/getAdmins")
    public AjaxResult getAdmins() {
        SysUser sysUser = new SysUser();
        sysUser.setRoleId(1L);
        List<SysUser> sysUsers = sysUserService.selectAllocatedList(sysUser);
        return AjaxResult.success(sysUsers);
    }

    /**
     * 省份、数量实体
     */
    @Data
    private class ProvinceCountVo implements Serializable {
        private static final long serialVersionUID = 1L;
        String name;
        Long value;

        public ProvinceCountVo(String name, Long value) {
            this.name = name;
            this.value = value;
        }

    }
}